<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button
            type="primary"
            size="small"
            @click="addPrmission({ type: 1, pid: 0 })"
          >添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template #default="{ row }">
              <el-button
                type="text"
                @click="addPrmission({ type: 2, pid: row.id })"
              >添加</el-button>
              <el-button type="text" @click="EditPermissions(row.id)">编辑</el-button>
              <el-button type="text" @click="RemovePermissions(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>
    <!-- 新增弹框 -->
    <el-dialog :visible="showDialog" title="弹层标题" @close="cliseDialog">
      <el-form ref="ruleForm" label-width="100px" :rules="rules" :model="formData">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-text="启用"
            active-value="1"
            inactive-text="不启用"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: right">
          <el-button @click="cliseDialog">取消</el-button>
          <el-button type="primary" @click="confirm">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  fetchPermissionAPI,
  updatePermissionAPI,
  RemovePermissionsAPI,
  AccessPermissionsAPI,
  EditPermissionsAPI
} from '@/api/permission'
import transTree from '@/utils/transTree'
export default {
  name: 'Permission',
  data() {
    const validatePassCode = (rule, value, callback) => {
      let updaeList = []
      // 先找相同的type字符然后返回新数组进行遍历筛选
      if (this.formData.id) {
        updaeList = this.codeList
          .filter(item => item.type === this.formData.type)
          .map(item => item.code)
          .filter(item => item !== this.catchCode)
      } else {
        updaeList = this.codeList
          .filter(item => item.type === this.formData.type)
          .map(item => item.code)
      }
      // console.log(updaeList)
      if (updaeList.includes(value)) {
        callback(new Error('权限点标识不可以重复'))
      } else {
        callback()
      }
    }
    const validatePassName = (rule, value, callback) => {
      let updaeList = []
      if (this.formData.id) {
        // 先找相同的type字符然后返回新数组进行遍历筛选
        updaeList = this.codeList
          .filter(item => item.type === this.formData.type)
          .map(item => item.name)
          .filter(item => item !== this.catchName)
      } else {
        updaeList = this.codeList
          .filter(item => item.type === this.formData.type)
          .map(item => {
            return item.name
          })
      }
      // console.log(updaeList)
      if (updaeList.includes(value)) {
        callback(new Error('名称不可以重复'))
      } else {
        callback()
      }
    }
    return {
      list: [],
      showDialog: false,
      formData: {
        enVisible: '', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: null, // 添加的权限类型 如果1级 type=1 如果2级 type =2
        pid: '' // 添加到哪个节点下  如果点击上面的 传固定的0 如果点击表格内 要以当行的id为主
      },
      codeList: [],
      catchName: null,
      catchCode: null,
      rules: {
        name: [
          { required: true, message: '请输入权限名字', trigger: 'blur' },
          { validator: validatePassName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '权限标识必填', trigger: 'blur' },
          { validator: validatePassCode, trigger: 'blur' }
        ],
        description: [{ required: true, message: '权限点,描述必填', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 获取员工
    async getList() {
      const res = await fetchPermissionAPI()
      // this.unames = res
      this.codeList = res
      this.list = transTree(res)
    },
    addPrmission({ type, pid }) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    // 弹框关闭清空数据项
    cliseDialog() {
      this.showDialog = false
      this.$refs.ruleForm.resetFields()
      this.formData = {
        enVisible: '', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: null, // 类型
        pid: '' // 添加到哪个节点下
      }
    },
    // 添加编辑修改权限
    confirm() {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          // 判断
          if (this.formData.id) {
            // 修改
            await EditPermissionsAPI(this.formData)
          } else {
            // 新增
            await updatePermissionAPI(this.formData)
          }
          this.$message.success(this.formData.id ? '修改成功' : '添加成功')
          this.getList()
          this.cliseDialog()
        }
      })
    },
    // 删除权限
    RemovePermissions(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await RemovePermissionsAPI(id)
        this.getList()
        this.$message.success('删除成功')
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      })
    },
    // 数据回显
    async EditPermissions(id) {
      const res = await AccessPermissionsAPI(id)
      this.showDialog = true
      this.formData = res
      this.catchName = res.name
      this.catchCode = res.code
    }
  }
}
</script>

<style></style>
